package test.wenlongmarket;

public class Market {
    String name;
    String address;
    Vip[] vip = new Vip[100];
    int count = 0;

    public void show() {
    }

    /**
     * 根据名字找这个人的位置，找不到返回-1
     * @param newcard
     * @return
     */
    public int find(String newcard){
        for (int i = 0; i < count; i++) {
            if (vip[i].getCard().equals(newcard)) {
                return i;
            }
        }
        return -1;
    }

    public boolean checkRepeat(String newcard) {
        for (int i = 0; i < count; i++) {
            if (vip[i].getCard().equals(newcard)) {
                return false;
            }
        }
        return true;
    }

    public boolean add(Vip newvip) {
        int weizhi  = find(newvip.getCard());
        if(weizhi<0){
            vip[count++] = newvip;
            return true;
        }
        return false;//重复了

//        if (!checkRepeat(newvip.getCard()) || count >= vip.length) {
//            System.out.println("无法加人");
//            return false;
//        }
//        vip[count++] = newvip;
//        return true;
    }

    public boolean addScore(String vipnum, int score) {
        for (int i = 0; i < count; i++) {
            if (vip[i].getCard().equals(vipnum)) {
                vip[i].setIntegration(vip[i].getIntegration() + score);
                return true;
            }
        }
        return false;
    }

    public String exchange(String vipnum, int choice) {
        int weizhi  = find(vipnum);
        if(weizhi<0){
            return null;
        }
        Vip v=vip[weizhi];//就是那个会员
        if (choice == 1) {
            if (v.getIntegration() >= 1000) {
               v.setIntegration(v.getIntegration() - 1000);
                return "砂锅";
            }
        } else if (choice == 2) {
            if (v.getIntegration() >= 400) {
               v.setIntegration(v.getIntegration() - 400);
                return "剃须刀";
            }
        } else if (choice == 3) {
            if (v.getIntegration() >= 100) {
                v.setIntegration(v.getIntegration() - 100);
                return "餐巾纸";
            }
        }
        return  null;


//        for (int i = 0; i < count; i++) {
//            if (vip[i].getCard().equals(vipnum)) {
//                if (choice == 1) {
//                    if (vip[i].getIntegration() >= 1000) {
//                        vip[i].setIntegration(vip[i].getIntegration() - 1000);
//                        return "砂锅";
//                    }
//                } else if (choice == 2) {
//                    if (vip[i].getIntegration() >= 400) {
//                        vip[i].setIntegration(vip[i].getIntegration() - 400);
//                        return "剃须刀";
//                    }
//                } else if (choice == 3) {
//                    if (vip[i].getIntegration() >= 100) {
//                        vip[i].setIntegration(vip[i].getIntegration() - 100);
//                        return "餐巾纸";
//                    }
//                }
//            }
//        }
//        return null;
    }
}

